package com.app.controller;

import com.app.model.AdminUserModel;
import com.app.model.TestFailureModel;
import com.app.model.TestsRecordModel;
import com.app.service.AdminUserService;
import com.app.service.TestFailureService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Controller
@RequestMapping("/api/tests")
public class TestFailureController {
    @Autowired
    private TestFailureService testFailureService;
    @Autowired
    private AdminUserService adminUserService;

    //失败的实验
    //新增和编辑
    @PostMapping("/addTestFailure")
    public Result<Object> addTestFailure(@RequestBody TestFailureModel testFailureModel) {
        try {
            if (testFailureModel.getId() > 0) {
                testFailureModel.setUpdateTime(CommonHelp.getNowTimestamp());
                testFailureService.update(testFailureModel);
            } else {
                testFailureModel.setCreateTime(CommonHelp.getNowTimestamp());
                testFailureService.insert(testFailureModel);
            }
        } catch (Exception e) {
            return Result.success(e);
        }
        return Result.success("操作成功");
    }

    //列表
    @GetMapping("/getTestFailureList")
    public Result<Object> getTestFailureList(Integer page, Integer limit, String name, Integer departmentId,Integer sortType) {
        return Result.success(testFailureService.getTestFailureList(page, limit, name, departmentId,sortType));
    }

    //删除
    @GetMapping("/deleteTestFailure")
    public Result<Object> deleteTestFailure(Integer id) {
        try {
            testFailureService.deleteTestFailure(id);
        } catch (Exception e) {
            return Result.success(e);
        }
        return Result.success("操作成功");
    }

    /**
     * 批量删除失败的实验数据
     *
     * @param ids
     * @param request
     * @return
     */
    @DeleteMapping("/deleteTestFailureBatchByIds")
    public Result<Object> deleteBatchById(@RequestParam("ids") String ids, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return testFailureService.deleteBatchByIds(ids);
    }


    /**
     * 根据ID获取试验记录
     *
     * @param request
     * @return
     */
    @GetMapping("/getTestsFailure")
    @ResponseBody
    public Result<Object> getTestsFailure(
            HttpServletRequest request, Integer id) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        TestFailureModel model = testFailureService.getModelById(id);
        return Result.success(model);
    }
}
